﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nature.Toolkit.GMix
{
    public abstract class ExpressionAdapter
    {
        protected abstract IExpression Parse(string expressionString);

        private Lazy<IExpression> m_expression;
        private System.Collections.IEnumerable m_entitiesHeap;
        
        public ExpressionAdapter(string expressionString, System.Collections.IEnumerable entitiesHeap)
        {
            m_expression = new Lazy<IExpression>(() => Parse(expressionString));
            m_entitiesHeap = entitiesHeap;
        }

        public IEnumerable<CompositionObject> this[string componentName]
        {
            get 
            {
                return 
                    from p in m_expression.Value.AsEnumerable(m_entitiesHeap)
                    from c in p.CompositionObjects
                    where 0 == StringComparer.InvariantCultureIgnoreCase.Compare(c.ComponentName, componentName)
                    select c;
            }
        }
    }
}
